/* jshint unused:false*/

+ function($) {
    "use strict";

    var today = new Date();

    var getDays = function(max) {
        var days = [];
        for(var i=1; i<= (max||31);i++) {
            days.push(i < 10 ? "0"+i : i);
        }
        return days;
    };

    var getDaysByMonthAndYear = function(month, year) {
        var int_d = new Date(year, parseInt(month)+1-1, 1);
        var d = new Date(int_d - 1);
        return getDays(d.getDate());
    };

    var formatNumber = function (n) {
        return n < 10 ? "0" + n : n;
    };

    var initMonthes = ('01 02 03 04 05 06 07 08 09 10 11 12').split(' ');

    var initYears = (function () {
        var arr = [];
        for (var i = 1980; i <= 2030; i++) { arr.push(i); }
        return arr;
    })();


    var defaults = {

        rotateEffect: false,  //为了性能

        value: [today.getFullYear(), formatNumber(today.getMonth()+1), formatNumber(today.getDate()), today.getHours(), formatNumber(today.getMinutes())],

        onChange: function (picker, values, displayValues) {
            var days = getDaysByMonthAndYear(picker.cols[1].value, picker.cols[0].value);
            //console.log();
            var currentValue = picker.cols[2].value;
            if(currentValue > days.length) currentValue = days.length;
            picker.cols[2].setValue(currentValue);
        },

        formatValue: function (p, values, displayValues) {
            return displayValues[0] + '-' + values[1] + '-' + values[2] + ' ' + values[3] + ':' + values[4];
        },
        setDateRange: function () {
            console.log(this.minDate);
            console.log(this.maxDate);
            //最小日期
            if(this.minDate){
                var minDateString =  this.minDate.split(' ');
                var minYearMonthDay = minDateString[0];
                var minYearMonthDayArr = minYearMonthDay.split('-'); //获取最小年月日
                var minHoursMintus = minDateString[1];
                var minHoursMintusArr =  minHoursMintus.split(':');//获取最小时分
            }
           // console.log(minYearMonthDayArr);
           // console.log(minHoursMintusArr);
            //最大日期
            if(this.maxDate){
                var maxDateString =  this.maxDate.split(' ');
                var maxYearMonthDay = maxDateString[0];
                var maxYearMonthDayArr = maxYearMonthDay.split('-');//获取最大年月日
                var maxHoursMintus = maxDateString[1];
                var maxHoursMintusArr =  maxHoursMintus.split(':');//获取最大时分
            }
            // console.log(maxYearMonthDayArr);
           // console.log(maxHoursMintusArr);
            //如果开始时间/结束时间都自己定义
            if(this.maxDate && this.minDate)
            {
                //处理年
                var arr = [];
                for (var i = minYearMonthDayArr[0]; i <= maxYearMonthDayArr[0]; i++) { arr.push(i); }
                this.cols[0].values=arr;

                //处理月
                var month = [];
                if(minYearMonthDayArr[1]<10){
                    minYearMonthDayArr[1] = parseInt(minYearMonthDayArr[1].substring(1));
                }
                if(maxYearMonthDayArr[1]<10){
                    maxYearMonthDayArr[1] = parseInt(maxYearMonthDayArr[1].substring(1));
                }
                console.log(minYearMonthDayArr[1]);
                console.log(maxYearMonthDayArr[1]);
                for(var i= minYearMonthDayArr[1]; i<= maxYearMonthDayArr[1];i++) {
                    month.push(i < 10 ? "0"+i : i);
                }
                this.cols[1].values =month;

                //处理日
                var days = [];
                if(minYearMonthDayArr[2]<10){
                    minYearMonthDayArr[2] = parseInt(minYearMonthDayArr[2].substring(1));
                }
                if(maxYearMonthDayArr[2]<10){
                    maxYearMonthDayArr[2] = parseInt(maxYearMonthDayArr[2].substring(1));
                }
                for(var i=minYearMonthDayArr[2]; i<= maxYearMonthDayArr[2];i++) {days.push(i < 10 ? "0"+i : i);}
                this.cols[2].values =days;

                //处理时
                var hours = [];
                if(minHoursMintusArr[0]<10){
                    minHoursMintusArr[0] = parseInt(minHoursMintusArr[0].substring(1));
                }
                if(maxHoursMintusArr[0]<10){
                    maxHoursMintusArr[0] = parseInt(maxHoursMintusArr[0].substring(1));
                }
                for (var i = minHoursMintusArr[0]; i <= maxHoursMintusArr[0]; i++) { hours.push(i); }
                this.cols[4].values =hours;

                //处理分
                var Minutes = [];
                if(minHoursMintusArr[1]<10){
                    minHoursMintusArr[1] = parseInt(minHoursMintusArr[1].substring(1));
                }
                if(maxHoursMintusArr[1]<10){
                    maxHoursMintusArr[1] = parseInt(maxHoursMintusArr[1].substring(1));
                }
                for (var i = minHoursMintusArr[1]; i <= maxHoursMintusArr[1]; i++) { Minutes.push(i < 10 ? '0' + i : i); }
                this.cols[6].values =Minutes;
            }
            //如果只定义最小时间 没定义最大时间
            if(this.minDate && this.maxDate==undefined){
                //处理年
                var arr = [];
                for (var i = minYearMonthDayArr[0]; i <= 2030; i++) { arr.push(i); }
                this.cols[0].values=arr;

                //处理月
                var month = [];
                if(minYearMonthDayArr[1]<10){
                    minYearMonthDayArr[1] = parseInt(minYearMonthDayArr[1].substring(1));
                }
                for(var i= minYearMonthDayArr[1]; i<=12;i++) {
                    month.push(i < 10 ? "0"+i : i);
                }
                this.cols[1].values =month;

                //处理日
                var days = [];
                if(minYearMonthDayArr[2]<10){
                    minYearMonthDayArr[2] = parseInt(minYearMonthDayArr[2].substring(1));
                }
                for(var i=minYearMonthDayArr[2]; i<= 31;i++) {days.push(i < 10 ? "0"+i : i);}
                this.cols[2].values =days;

                //处理时
                var hours = [];
                if(minHoursMintusArr[0]<10){
                    minHoursMintusArr[0] = parseInt(minHoursMintusArr[0].substring(1));
                }
                for (var i = minHoursMintusArr[0]; i <= 23; i++) { hours.push(i); }
                this.cols[4].values =hours;

                //处理分
                var Minutes = [];
                if(minHoursMintusArr[1]<10){
                    minHoursMintusArr[1] = parseInt(minHoursMintusArr[1].substring(1));
                }
                for (var i = minHoursMintusArr[1]; i <= 59; i++) { Minutes.push(i < 10 ? '0' + i : i); }
                this.cols[6].values =Minutes;
            }
            //如果只定义最大时间 没定义最小时间
            if(this.maxDate && this.minDate==undefined){
                //处理年
                var arr = [];
                for (var i = 1980; i <= maxYearMonthDayArr[0]; i++) { arr.push(i); }
                this.cols[0].values=arr;

                //处理月
                var month = [];
                if(maxYearMonthDayArr[1]<10){
                    maxYearMonthDayArr[1] = parseInt(maxYearMonthDayArr[1].substring(1));
                }
                for(var i= 1; i<= maxYearMonthDayArr[1];i++) {
                    month.push(i < 10 ? "0"+i : i);
                }
                this.cols[1].values =month;

                //处理日
                var days = [];
                if(maxYearMonthDayArr[2]<10){
                    maxYearMonthDayArr[2] = parseInt(maxYearMonthDayArr[2].substring(1));
                }
                for(var i=1; i<= maxYearMonthDayArr[2];i++) {days.push(i < 10 ? "0"+i : i);}
                this.cols[2].values =days;

                //处理时
                var hours = [];
                if(maxHoursMintusArr[0]<10){
                    maxHoursMintusArr[0] = parseInt(maxHoursMintusArr[0].substring(1));
                }
                for (var i = 1; i <= maxHoursMintusArr[0]; i++) { hours.push(i); }
                this.cols[4].values =hours;

                //处理分
                var Minutes = [];
                if(maxHoursMintusArr[1]<10){
                    maxHoursMintusArr[1] = parseInt(maxHoursMintusArr[1].substring(1));
                }
                for (var i = 1; i <= maxHoursMintusArr[1]; i++) { Minutes.push(i < 10 ? '0' + i : i); }
                this.cols[6].values =Minutes;
            }
           if(this.maxDate==undefined && this.minDate==undefined)
            {
                var arr = [];
                for (var i = 1950; i <= 2030; i++) { arr.push(i); }
                this.cols[0].values=arr;

                //处理月
                var month = [];
                for(var i= 1; i<= 12;i++) {
                    month.push(i < 10 ? "0"+i : i);
                }
                this.cols[1].values =month;

                //处理日
                var days = [];
                for(var i=1; i<= 31;i++) {days.push(i < 10 ? "0"+i : i);}
                this.cols[2].values =days;

                //处理时
                var hours = [];
                for (var i = 1; i <= 23; i++) { hours.push(i); }
                this.cols[4].values =hours;

                //处理分
                var Minutes = [];
                for (var i = 1; i <= 59; i++) { Minutes.push(i < 10 ? '0' + i : i); }
                this.cols[6].values =Minutes;
            }

            //如果是false  说明小时不显示
            if(this.showHours==false){
                this.cols[4].values =[];
            }

            //如果是false  说明分钟不显示
            if(this.showMinuite==false){
                this.cols[5].content ='';
                this.cols[6].values =[];
            }
            console.log(this);
        },
        cols: [
            // Years
            {
                values: []//initYears
            },
            // Months
            {
                values: []//initMonthes
            },
            // Days
            {
                values: []//getDays()
            },

            // Space divider
            {
                divider: true,
                content: '  '
            },
            // Hours
            {
                values: []/*(function () {
                    var arr = [];
                    for (var i = 0; i <= 23; i++) { arr.push(i); }
                    return arr;
                })()*/
            },
            // Divider
            {
                divider: true,
                content: ':'
            },
            // Minutes
            {
                values: []/*(function () {
                    var arr = [];
                    for (var i = 0; i <= 59; i++) { arr.push(i < 10 ? '0' + i : i); }
                    return arr;
                })()*/
            }
        ]
    };

    /*
     $("#s_datetime-picker").datetimePicker(
     {
     value: ['1985', '12', '04', '9', '34'],
     yearmin:'1980'
     }
     );
     */
    $.fn.datetimePickerCustom = function(params) {

        return this.each(function() {
            if(!this) return;
            var p = $.extend(defaults, params);
            p.setDateRange();//新增方法,处理时间日期范围
            $(this).picker(p);
            if (params.value) $(this).val(p.formatValue(p, p.value, p.value));
        });
    };

}(Zepto);